package com.ys.peony.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.ys.peony.R;
import com.ys.peony.ui.activity.TakePicturesActivity;
import com.ys.peony.utils.FileManager;
import com.ys.peony.utils.LogUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.squareup.picasso.MemoryPolicy.NO_CACHE;
import static com.squareup.picasso.MemoryPolicy.NO_STORE;

public class TakePicPreviewAdapter extends BaseAdapter{
	private TakePicturesActivity mContext;
	private List<String> picList;

	public TakePicPreviewAdapter(TakePicturesActivity context, ArrayList<String> picList){
		this.mContext = context;
		this.picList=picList;
	}
	@Override
	public int getCount() {
		return picList.size();
	}
	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if(convertView==null){
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.preview_for_takepic_item_layout, null);
			holder.previewImg=(ImageView)convertView.findViewById(R.id.preview_img);
			holder.delImgBtn=(ImageButton)convertView.findViewById(R.id.del_imgbtn);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder)convertView.getTag();
		}
		LogUtil.i("aaaa","______"+position+"____"+picList.get(position));
		Picasso.with(mContext).load(new File(picList.get(position)))
				.resize(160, 160)
				.centerInside()
				.memoryPolicy(NO_CACHE, NO_STORE)
				.into(holder.previewImg);
		holder.delImgBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				FileManager.delete(picList.get(position));
				mContext.removeImg(position);
			}
		});

		return convertView;
	}

	private static class ViewHolder {
		private ImageButton delImgBtn ;
		private ImageView previewImg;
	}

	public void setDate(ArrayList<String> list){
		this.picList=list;
		notifyDataSetChanged();
	}
}